<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :add="add" />
      <List :todos="todos" :del="del" :Check="Check" />
      <Footer :todos="todos" :SelectAll="SelectAll" :DeleteAll="DeleteAll" />
    </div>
  </div>
</template>

<script>
// 引入组件
import List from "./components/List.vue";
import Header from "./components/Header";
import Footer from "./components/Footer";
export default {
  name: "App",
  // 注册
  components: { List, Header, Footer },
  // 数据
  data() {
    return {
      todos: [],
    };
  },
  // 方法
  methods: {
    // 1.添加的方法
    add(todoValue) {
      this.todos.unshift(todoValue);
    },
    // 2.单个删除的方法 接受需要删除的id
    del(id) {
      // 判断不等于的id并重新渲染页面
      this.todos = this.todos.filter((item) => item.id !== id);
    },
    // 3.单个check修改的状态
    Check(id) {
      this.todos = this.todos.map((item) =>
        item.id === id ? { ...item, done: !item.done } : item
      );
    },
    // 4.全选的函数方法
    SelectAll(value) {
      this.todos = this.todos.map((item) => ({ ...item, done: value }));
    },
    // 选中删除的
    DeleteAll() {
      this.todos = this.todos.filter((item) => !item.done);
    },
  },
  // 监听属性
  watch: {
    todos: {
      // 开启深度监听
      deep: true,
      handler() {
        localStorage.setItem("todos", JSON.stringify(this.todos));
      },
    },
  },
  //挂载完成后获取数据
  mounted() {
    this.todos = JSON.parse(localStorage.getItem("todos") || "[]");
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>